import { clearTimeout } from "timers";
import { getCheckDetail, selectMyPmById } from "../../api/manage"
import { getUserInfoByUserDDId } from "../../api/index"
Page({
  data: {
    checkGroup: [],// 人员数据list
    stagCode: '',// 步骤码
    formid: '',// 唯一标识
    contentText: {
      contentdown: "上拉显示更多",
      contentrefresh: "正在加载...",
      contentnomore: "没有更多数据了"
    },// 文字
    loadingType: 0,// 文字显示状态
    size: 10,// 显示条数
    hasMore: true, // 是否还有更多数据
    isLoading: false, // 是否正在加载中
    page: 1,// 初始页数
    total: 0,// 初始总数
    searchKeyword: '',//输入框的值
    inputGroup: [],//输入框数组
    hasInputMore: true, // 是否还有更多数据
    isLoadingInput: false, // 是否正在加载中
    inputPage: 1,
    inputTotal: 0,
    timerOut: null,
    period: ''
  },
  onLoad(options) {
    this.setData({
      stagCode: options.tagid == 'null' || options.tagid == undefined ? '' : options.tagid,
      formid: options.pmid == 'null' || options.pmid == undefined ? '' : options.pmid,
      period: options.period == 'null' || options.period == undefined ? '' : options.period,
    })
    this.loadData(1, true)
  },
  loadData(page, refresh = false) {
    this.setData({
      isLoading: true,
      loadingType: 1
    })
    let data = {
      page: page,
      size: this.data.size,
      pmStageCode: this.data.stagCode,
      pmFormId: this.data.formid,
      pmPeriodCode: this.data.period
    }
    getCheckDetail(data).then(res => {
      const list = res.data.list;
      const hasMore = list.length >= this.data.size;
      this.setData({
        checkGroup: refresh ? list : this.data.checkGroup.concat(list),
        inputArry: refresh ? list : this.data.checkGroup.concat(list),
        page,
        hasMore,
        isLoading: false,
        loadingType: 2,
        total: res.data.total,
        inputTotal: res.data.total
      });
    })
  },
  loadInputData(page, refresh = false, name) {
    this.setData({
      isLoadingInput: true,
      loadingType: 1
    })
    let data = {
      page: page,
      size: this.data.size,
      pmStageCode: this.data.stagCode,
      pmFormId: this.data.formid,
      pmUserName: name
    }
    getCheckDetail(data).then(res => {
      const list = res.data.list;
      const hasInputMore = list.length >= this.data.size;

      this.setData({
        checkGroup: refresh ? list : this.data.checkGroup.concat(list),
        page,
        hasInputMore,
        isLoadingInput: false,
        loadingType: 2,
        total: res.data.total,

      });

    })
  },
  // 模糊匹配
  searchStaff(value, e) {
    this.setData({
      searchKeyword: value,

    })
    if (!value.trim()) {
      this.setData({
        checkGroup: this.data.inputArry,
        total: this.data.inputTotal
      })
      // 值不合法还是原来的数据
      return
    }
    this.loadInputData(1, true, value)
    // 请求匹配过的数据
    // this.debounce(this.loadInputData(1, true, value), 10000)
    // this.loadInputData(1, true, value)
    // if (this.data.timerOut !== null) { clearTimeout(this.data.timerOut) }
    // this.data.timerOut = setTimeout(() => {
    //   this.loadInputData(1, true, value)
    //   console.log(value.trim(), 'trim');
    // }, 300)

    // if (value) {
    //   this.loadInputData(1, true, value)
    // } else {

    // }
    // this.light()
    // if (value) {
    //   console.log('输入时请求');
    //   this.getSeachData(value)
    // } else {
    //   console.log('没值的时候是this.getUserData()');
    //   this.setData({
    //     checkGroup: this.data.demo,
    //     total: this.data.otherTotal
    //   })
    // }
  },
  light: (function () {
    let i = null
    return function () {   // 接收携带的参数在这
      // if (i != null) clearTimeout(i)
      i = setTimeout(() => {
      }, 1000)
    }
  })(),
  checkMore(e) {
    // dd.navigateTo({
    //   url: '/pages/usual/performancedetail/checkdetail?tagid=' + tagid + '&pmid=' + pmids + '&count=' + count
    // })

    let { pmid, userName, avatar, dept } = e.currentTarget.dataset

    console.log(e.currentTarget.dataset);
    let gData1 = {
      pmId: pmid
    }
    let gData2 = {
      userDDId: ''
    }
    let navParamData = {
      pmId: pmid,
      userName: "",
      avatar: "",
      pmDeptName: ""
    }



    // 根据pmId查询绩效信息
    selectMyPmById(gData1).then((res) => {
      console.log('查询绩效信息', res);
      gData2.userDDId = res.data.pmUserDDId
      navParamData.pmDeptName = res.data.pmDeptName

      // 根据该绩效的归属者userDDId查询用户信息
      getUserInfoByUserDDId(gData2).then((res1) => {
        console.log('该绩效的用户信息', res1);
        navParamData.userName = res1.data.userName
        navParamData.avatar = res1.data.avatar

        // 进行跳转页面
        dd.navigateTo({
          url: `/pages/usual/performancedetail/performancedetail?pmid=${navParamData.pmId}&userName=${navParamData.userName
            }&avatar=${navParamData.avatar}&dept=${navParamData.pmDeptName}&userId=${res1.data.userId}&managerUserId=${res1.data.managerUserDDId}`,
        })
      })
    })


  },
  // 上拉加载数据
  onReachBottom() {
    if (this.data.searchKeyword) {
      if (this.data.hasInputMore && !this.data.isLoadingInput) {
        this.loadInputData(this.data.page + 1, false, this.data.searchKeyword)
      }
    } else {
      if (this.data.hasMore && !this.data.isLoading) {
        this.loadData(this.data.page + 1)
      }
    }
  }
});
